<?php


namespace app\admin\controller;

use app\common\model\V_fsj_data;
use app\common\model\Fsj;
use excel\excelbase;
use think\facade\Request;

class Weeks extends Base
{
    public function excel() {
        //生成EXCEL表格


        //$data=[

        // ['嘿嘿','heihei'],

        //['哈哈','haha']

        //];
        $input_date=Request::param('input_date');
        $fst=Request::param('fst');


        $rows = \app\wx\model\V_fsj_data::field('  id,work_fre ,in_power,back_power,zhubo,amb_v,amb_a,main_signal,back_signal,content,imgs,fst_name,fst_pd ,name,title')
            ->where('fst_id','=',$fst)
            ->where('title','=', $input_date)
            //->where('DATE_FORMAT( add_time, \'%Y%m\' )','=',$t_month )
            ->order(['id'=>'desc'])
            ->select()
            -> toArray();

        $name=$input_date.'_'.$fst.'报表';

        $header=['编号','发射台','发射机所属频道','日期','工作频率(MHZ)','入射功率(KW)','反射功率(W)','驻波比','功放电压(V)','功放电流(A)' ,'主用信号源' ,'备用信号源','备注','提交人'];
        $keys=['id','fst_name','fst_pd','title','work_fre','in_power','back_power','zhubo','amb_v','amb_a' ,'main_signal' ,'back_signal','content','name'];


        $myexcel= new excelbase();
        $myexcel->outdata($name, $rows, $header, $keys);
        //$myexcel->excelExport($name,$header,$data);

    }
    public  function  get_detail()
    {//获取周报详细信息


        $id=input('post.id');
        $title=input('post.title');


        $data = V_fsj_data::field('  id,work_fre ,in_power,back_power,zhubo,amb_v,amb_a,main_signal,back_signal,content,imgs,fst_name,fst_pd ,name,title')
            ->where('fst_id','=',$id)
            ->where('title','=', $title)
            //->where('DATE_FORMAT( add_time, \'%Y%m\' )','=',$t_month )
            ->order(['id'=>'desc'])
            ->select()
            -> toArray();


        //返回判断
        if ($data){
            $msg='OK';
            $status=200;
        }else{
            $msg='failed';
            $status=0;
        }
        $meta=[
            'msg'=>$msg,
            'status'=>$status
        ];
        $res=[
            'data'=>$data,
            'meta'=>$meta
        ];
        return json($res);
    }

    public  function get_list()
    { //获取发射台列表

        $pagenum=input('post.pagenum'); //// 当前页码数
        $pagesize=input('post.pagesize'); //// 当前每页有多少条
        $fst_id=input('post.fst_id');
        $rows = V_fsj_data::field('title,fst_id')
            ->where('fst_id','=',$fst_id)
            ->group('title,fst_id')
            ->order(['id'=>'desc'])
            ->paginate([
                'list_rows'=> $pagesize,
                'page' => $pagenum,
            ])
            ->each(function($item, $key){ //为返回的数据集添加新数据项
                $item->num = Fsj::field(' id')
                    ->where('fst_id',$item['fst_id'])
                    ->select()
                    ->count();
                $item->todo = V_fsj_data::field(' id')
                    ->where('fst_id',$item['fst_id'])
                    ->where('title',$item['title'])
                    ->select()
                    ->count();
            })
            ->toArray();
        //返回判断
        if ($rows){
            $msg='OK';
            $status=200;
        }else{
            $msg='failed';
            $status=0;
        }
        $meta=[
            'msg'=>$msg,
            'status'=>$status
        ];
        $res=[
            'data'=>$rows,
            'meta'=>$meta
        ];
        return json($res);
    }
}
